<template>
  <div id="member-list" class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleFilter()" size="small">查询搜索</el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetFilter()"
          size="small"
        >重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入昵称：">
            <el-input v-model="listQuery.nickName" class="input-width" placeholder="昵称" clearable></el-input>
          </el-form-item>
          <el-form-item label="输入手机号：">
            <el-input v-model="listQuery.phone" class="input-width" placeholder="手机号" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!--disabled值动态显示，默认为true,当选中复选框后值为false-->
      <el-button
        type="danger"
        @click="deleteBatch"
        :disabled="this.multipleSelection.length === 0"
        style="float:right"
      >批量删除</el-button>
    </el-card>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      ref="memberTable"
      @selection-change="handleSelectionChange"
        @sort-change="sortChange"
      style="width: 100%;"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="编号" v-if="colData[0].istrue"  prop="id" sortable="custom" align="center" width="80px" :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
       <el-table-column  label="会员头像"  v-if="colData[1].istrue" width="110px" align="center">
        <template slot-scope="{row}">
          <img :src="row.avatarUrl" alt="" style="width: 40px; max-height: 40px; vertical-align: middle;">
        </template>
      </el-table-column>
      <el-table-column label="会员昵称" v-if="colData[2].istrue" min-width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" v-if="colData[3].istrue" min-width="50px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.gender === 0">未知</span>
          <span v-if="row.gender === 1">男</span>
          <span v-if="row.gender === 2">女</span>
        </template>
      </el-table-column>

      <el-table-column label="手机号码"  v-if="colData[4].istrue" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员积分" v-if="colData[5].istrue" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.integral }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员折扣" v-if="colData[6].istrue" min-width="80px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.discount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账户余额" v-if="colData[7].istrue" min-width="80px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.balance }}¥</span>
        </template>
      </el-table-column>
      <el-table-column label="会员邮箱" v-if="colData[8].istrue" min-width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column  label="创建时间" v-if="colData[9].istrue" prop="createdTime" sortable="custom" min-width="160px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createdTime }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" width="240" class-name="operation">
        <template
          slot="header"
          >
          操作
          <el-popover
            id="el-popover"
            placement="right"
            title="列筛选"
            width="100"
            trigger="click">
            <el-checkbox-group id="checkbox-group" v-model="colOptions" >
              <el-checkbox   v-for="(item,index) in colSelect" :label="item" :key="item"/>
            </el-checkbox-group>
            <span size="small" slot="reference"> <i class="el-icon-s-operation"/></span>
          </el-popover>
        </template>
        <template slot-scope="{row}">
          <el-link type="info" @click="skipAddressList(row.id)">查看地址</el-link>
          <el-link type="warning" @click="handUpdate(row.id,row.avatarUrl)">修改</el-link>
          <el-link type="danger" @click="handleDelete(row.id, row.nickName)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

<!-- 分页设置 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageIndex"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <member-handle is-detail ref="detailPage" />
    <member-handle is-add ref="addPage" @change="handleFilter" />
    <member-handle is-update ref="updatePage" @change="getList" />
  </div>
</template>

<script>
import waves from "@/directive/waves";
import Pagination from "@/components/Pagination";

import MemberHandle from "./components/member-handle";
import AddressHandle from "./components/address-handle";

import memberApi from "@/api/system/member-api";

/* 默认查询参数 */
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  nickName: null,
  phone: null
};

export default {
  name: "MemberList",
  components: { MemberHandle, Pagination, AddressHandle },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      sortColumn: "id",
      sortAsc: false,
      multipleSelection: [],
        colData: [
          { title: "编号", istrue: true },
          { title: "会员头像", istrue: true },
          { title: "会员昵称", istrue: true },
          { title: "性别", istrue: true },
          { title: "手机号码", istrue: true },
          { title: "会员积分", istrue: true },
          { title: "会员折扣", istrue: true },
          { title: "账户余额", istrue: true },
          { title: "会员邮箱", istrue: true },
          { title: "创建时间", istrue: true }
        ],
         colSelect: [],
        colOptions: [],
      listQuery: {
        pageIndex: 1,
        pageSize: 10,
        nickName: null,
        phone: null,
        pageSorts: []
      },
      tableColumnChecked: null,
      createTimeRange: null,
      downloadLoading: false
    };
  },
  /* 动态显示表格的列 */
  watch: {
      colOptions(valArr) {
        var arr = this.colSelect.filter(i => valArr.indexOf(i) < 0); // 未选中
        this.colData.filter(i => {
          if (arr.indexOf(i.title) !== -1) {
            i.istrue = false;
            this.$nextTick(() => {
              this.$refs.memberTable.doLayout();
            });
          } else {
            i.istrue = true;
            this.$nextTick(() => {
              this.$refs.memberTable.doLayout();
            });
          }
        });
      }
    },
  created() {
     this.iniTable()
    this.setDefaultSort();
    this.getList();
  },
  methods: {
    iniTable() {
        var _this = this;
        for (let i = 0; i < _this.colData.length; i++) {
          _this.colSelect.push(_this.colData[i].title);
         /*  初始化不想展示的列可以放在这个条件里 */
          if (_this.colData[i].title === '手机号码'
           || _this.colData[i].title === '创建时间'
           || _this.colData[i].title === '会员邮箱'
           ) {
            continue;
          }
          _this.colOptions.push(_this.colData[i].title);
        }
      },
    getList() {
      this.listLoading = true;
      memberApi.getPageList(this.listQuery).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.pageIndex = 1;
      this.getList();
    },

    /* 点击后跳到地址列表 */
    skipAddressList(memberId) {
       this.$router.push({
        path: '/address-list',
        query: {memberId: memberId}
      });
    },
    /* 重置查询 */
    handleResetFilter() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
    },
    handleCreateTimeFilter() {
      this.handleFilter();
    },
    handleClear() {
      this.handleFilter();
    },
    /*  设置默认排序 */
    setDefaultSort() {
      this.listQuery.pageSorts = [
        {
          column: this.sortColumn,
          asc: this.sortAsc
        }
      ];
    },
    sortChange(data) {
       const { prop, order } = data
        if (prop === 'creatdTime') {
          this.sortColumn = 'created_time'
        } else {
          this.sortColumn = prop
        }
        this.sortAsc = order === 'ascending'
        this.listQuery.pageSorts = [{
          column: this.sortColumn,
          asc: this.sortAsc
        }]
        this.handleFilter()
      },
    getSortClass: function(key) {
        if (this.sortColumn === key) {
          return this.sortAsc ? 'ascending' : 'descending'
        } else {
          return ''
        }
      },

    handDetail(id) {
      this.$nextTick(() => {
        this.$refs.detailPage.handle(id);
      });
    },
    handUpdate(id,avatarUrl) {
      this.$nextTick(() => {
        this.$refs.updatePage.handle(id,avatarUrl);
      });
    },
    /* 添加会员收货地址 */
    addAddress(memberId) {
      this.$nextTick(() => {
        this.$refs.addressPage.handle(memberId);
      });
    },

    handleDelete(id, nickName) {
      this.$confirm("您确定要删除 " + nickName + " ?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        memberApi.delete(id).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.handleFilter();
        });
      });
    },
    //操作多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /* 批量删除 */
    deleteBatch() {
      var ids = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        ids.push(this.multipleSelection[i].id);
      }
      this.$confirm("您确定要批量删除这些地址 ?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        memberApi.detdeleteBatchIds(ids).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.handleFilter();
        });
      });
    }
  }
};
</script>

<style lang="scss">
#member-list .el-table th,
.el-table td {
  padding: 6px 0px;
}

#member-list .filter-container {
  padding-bottom: 5px;
}

#member-list .filter-container .tree-select-item {
  width: 120px;
  display: inline-block;
  vertical-align: top;
  margin-right: 4px;
}

#member-list .input-with-select {
  vertical-align: top;
  width: 505px;
  margin-right: 4px;
}

#member-list .input-with-select .el-select .el-input {
  width: 120px;
}

#member-list .input-with-select .el-input-group__prepend {
  background-color: #fff;
}

#member-list .filter-item {
  margin-right: 4px;
}

#member-list .el-table__body .operation .cell {
  text-align: center;
}
</style>
